﻿Option Explicit On
Option Strict On


Public Class ACOM2000Simulator

    Private WithEvents m_VirtualACOM2000 As VirtualACOM2000

    Private WithEvents m_Timer As New Timers.Timer(100)

    Private Sub cmdStartStop_Click(sender As Object, e As EventArgs) Handles cmdStartStop.Click

        If m_VirtualACOM2000 Is Nothing Then
            cmdStartStop.Text = "Stop"
            m_VirtualACOM2000 = New VirtualACOM2000
            m_VirtualACOM2000.Start(txtCOMPort.Text)
            m_Timer.Start()
        Else
            cmdStartStop.Text = "Start"
            m_VirtualACOM2000.Stop()
            m_VirtualACOM2000 = Nothing
            m_Timer.Stop()
        End If

    End Sub


    Private Sub m_VirtualACOM2000_COMDataReceivedEvent(sender As Object, e As ACOMDirector.Hardware.ACOM2000.UnknownMessageEventArgs) Handles m_VirtualACOM2000.COMDataReceivedEvent

        Dim UpdateDelegate As UpdateCOMDataDelegate = New UpdateCOMDataDelegate(AddressOf UpdateCOMData)
        Dim arx As IAsyncResult = Me.BeginInvoke(UpdateDelegate, New Object() {e.Message})
        Me.EndInvoke(arx)



        
    End Sub

    Private Delegate Sub UpdateCOMDataDelegate(message As String)
    Private Sub UpdateCOMData(ByVal message As String)
        lstCOMPortData.Items.Add(message)

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        m_VirtualACOM2000.SendRandom("TEst Hello")
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        m_VirtualACOM2000.m_CurrentSegment = 169
        m_VirtualACOM2000.SendCurrentFrequencySegmentInfo_W7()
       
    End Sub

    Private Sub RandomForwardPower()

        Dim r As New Random()
        Dim rInt As Integer = r.Next(0, 16)

        m_VirtualACOM2000.SendForwardPower(rInt)

    End Sub

    Private Sub RandomReflectedPower()

        Dim r As New Random()
        Dim rInt As Integer = r.Next(0, 8)

        m_VirtualACOM2000.SendReflectedPower(rInt)

    End Sub

    Private Sub m_Timer_Elapsed(sender As Object, e As Timers.ElapsedEventArgs) Handles m_Timer.Elapsed
        RandomForwardPower()
        RandomReflectedPower()
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        m_VirtualACOM2000.m_CurrentSegment = 168
        m_VirtualACOM2000.SendCurrentFrequencySegmentInfo_W7()
    End Sub
End Class
